﻿from django.db import models

# Create your models here.

import datetime

from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _l
from django.utils.translation import ugettext as _
from django.forms import ModelForm
# from captcha.fields import CaptchaField

from captcha.fields import ReCaptchaField

from .models import VpnUser





class HumanForm(forms.Form):
    captcha = ReCaptchaField(
    public_key='6LftZOgSAAAAAPpBlfobmK0MFx7zfmiJ0B9G2nY9',
    private_key='6LftZOgSAAAAAPURM9GJ1LHmmMynRXnqDLsdEaMu',
    use_ssl=True,attrs={'theme' : 'white'}
    )


class SingUpForm(forms.Form):
    username = forms.CharField(max_length=16,widget=forms.TextInput(attrs={
        'class':'form-control',
        'placeholder':_('Username')}))
    email = forms.EmailField(widget=forms.TextInput(attrs={
        'class':'form-control',
        'placeholder':_('Email Address')}))

    password_first = forms.CharField(max_length=16,min_length=8,widget=forms.PasswordInput(attrs={
        'class':'form-control',
        'placeholder':_('Password')}))
    password_again = forms.CharField(max_length=16,min_length=8,widget=forms.PasswordInput(attrs={
        'class':'form-control',
        'placeholder':_('Confirm Password')}))

    first_name = forms.CharField(max_length=16,widget=forms.TextInput(attrs={
        'class':'form-control',
        'placeholder':_('First Name')}))
    last_name = forms.CharField(max_length=16,widget=forms.TextInput(attrs={
        'class':'form-control',
        'placeholder':_('Last Name')}))

    # captcha = CaptchaField()


    # agree = forms.BooleanField()
    # cc_myself = forms.BooleanField(required=False)

    def clean_username(self):
        username = self.cleaned_data['username']
        if User.objects.filter(username=username).exists():
            raise ValidationError(_("Username already exists"))
        return username

    def clean_email(self):
        email = self.cleaned_data['email']
        if User.objects.filter(email=email).exists():
            raise ValidationError(_("Email already exists"))
        return email


    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')

        if password != password2:
            raise forms.ValidationError(_("Your passwords do not match"))
        return password2


class SingInForm(forms.Form):
    username = forms.CharField(max_length=16,widget=forms.TextInput(attrs={
        'class':'form-control',
        'placeholder':_('Username')}))
    password = forms.CharField(max_length=16,widget=forms.PasswordInput(attrs={
        'class':'form-control',
        'placeholder':_("Password")}))


    def clean(self):
        cleaned_data = super(SingInForm, self).clean()
        username = cleaned_data.get('username')
        password = cleaned_data.get('password')
        from django.contrib.auth import authenticate
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                return cleaned_data
            else:
                raise forms.ValidationError(_("The password is valid, but the account has been disabled!"))
        else:
            raise forms.ValidationError(_("The username and password were incorrect."))

class ResetPasswordForm(forms.Form):

    email = forms.EmailField(widget=forms.TextInput(attrs={
        'class':'form-control',
        'placeholder':_('Email Address')}))


    def clean_email(self):
        email = self.cleaned_data['email']
        if User.objects.filter(email=email).exists() is False:
            raise ValidationError(_("Email is incorrect"))
        return email


class EditEmailForm(ModelForm):

    class Meta:
        model = User
        fields = ['email']
        widgets = {
            'email': forms.TextInput(attrs={'class':'form-control','placeholder':_l('Email Address')}),
        }

class EditProfileForm(ModelForm):

    class Meta:
        model = User
        fields = ['first_name','last_name']
        widgets = {
            'first_name': forms.TextInput(attrs={'class':'form-control','placeholder':_l('First Name')}),
            'last_name': forms.TextInput(attrs={'class':'form-control','placeholder':_l('Last Name')}),
        }


class EditPasswordForm(ModelForm):
    password2 = forms.CharField(max_length=16,min_length=8,label= _("Confirm Password"),widget=forms.PasswordInput(attrs={ 'class':'form-control','placeholder':_l('Confirm Password')}))

    class Meta:
        model = User
        fields = ['password']
        widgets = {
            'password': forms.PasswordInput(attrs={'class':'form-control','placeholder':_l('Password')}),
        }

    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')

        if password != password2:
            raise forms.ValidationError(_("Your passwords do not match"))
        return password2

    def save(self, commit=True):
        user = super(EditPasswordForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password"])
        if commit:
            user.save()
        return user


class EditBillingAddressForm(ModelForm):

    class Meta:
        model = VpnUser
        fields = ['country','province','city','address','address2','zipcode','telephone']
        widgets = {
            'country': forms.TextInput(attrs={'class':'form-control','placeholder':_l('Country')}),
            'province': forms.TextInput(attrs={'class':'form-control','placeholder':_l('Province')}),
            'city': forms.TextInput(attrs={'class':'form-control','placeholder':_l('City')}),
            'address': forms.TextInput(attrs={'class':'form-control','placeholder':_l('Address')}),
            'address2': forms.TextInput(attrs={'class':'form-control','placeholder':_l('Address2')}),
            'zipcode': forms.TextInput(attrs={'class':'form-control','placeholder':_l('zipcode')}),
            'telephone': forms.TextInput(attrs={'class':'form-control','placeholder':_l('Telephone')}),
        }
        labels = {
            'country': _('Country'),
            'province': _('Province'),
            'city': _('City'),
            'address': _('Address'),
            'address2': _('Address22'),
            'zipcode': _('Zipcode'),
            'telephone': _('Telephone'),
        }